<?php
/**
 * Created by PhpStorm.
 * User: I5
 * Date: 2020/11/26
 * Time: 10:12
 */

namespace app\api\controller\v1;

use app\api\controller\Base;
use app\circle\model\Circle;
use app\goods\model\Category;
use app\goods\model\Goods;
use service\ApiReturn;
use Think\Db;

class Home extends Base
{
    /*
     * 首页数据 
     *
     */
    public function index($data, $user)
    {
        //商品分类
        $catagory = $this->_category();
//        echo "<pre>";
//        print_r($catagory);die;
        //广告轮播
        $ad = $this->_ad(1);
        //菜单
        $menu = $this->_menu();
        //秒杀
        //$seckill = $this->_seckill();
        //发现好物
        $find = $this->_ad(2);
        //限时特卖
        $limit = $this->_ad(3);
        //热卖榜单
        $hot = $this->_hot();
        //新品首发
        $new = $this->_new();
        //新人0元购
        $buy = $this->_buy($data, $user);
        $res = [
            'catagory' => $catagory,
            'ad' => $ad,
            'menu' => $menu,
            //'seckill' => $seckill,
            'find' => $find,
            'limit' => $limit,
            'hot' => $hot,
            'new' => $new,
            'buy' => $buy
        ];
        return ApiReturn::r(1, $res, lang('首页数据'));
    }

    /*
     * 商品分类
     *
     */
    public function _category()
    {
        return Category::where([
            'is_show' => 1,
            'status' => 1,
//            'is_hot' => 1,
            'pid' => 0
        ])->select();
    }

    /*
     * type
     *
     */
    public function _type($type)
    {
        $data = [
            1 => lang('首页顶部轮播'),
            2 => lang('发现好物'),
            3 => lang('限时特卖')
        ];
        return $data[$type];
    }

    /*
     *广告轮播
     *
     */
    public function _ad($type)
    {
        $type = $this->_type($type);
        $ads = \app\operation\model\AdsType::alias('at')
            ->field('at.name as type_name,u.id as thumb,a.href,a.width,a.height')
            ->join('operation_ads a', 'at.id=a.typeid')
            ->leftJoin('__UPLOAD__ u', 'u.id=a.thumb')
            ->where('at.status', 1)
            ->where('a.status', 1)
            ->where('at.name', $type)
            ->select();
        if ($ads) {
            foreach ($ads as &$v) {
                $v['thumb'] = get_file_url($v['thumb']);
            }
        }
        return $ads;
    }

    /*
     *菜单
     *
     */
    public function _menu()
    {
        $menu = \app\operation\model\AdsType::alias('nt')
            ->field('nt.name as type_name,n.name,n.thumb,n.href')
            ->join('operation_nav n', 'nt.id=n.typeid')
            ->where('nt.status', 1)
            ->where('n.status', 1)
            ->order('n.sort desc')
            ->select();
        foreach ($menu as &$vo) {
            $vo['thumb'] = get_thumb($vo['thumb']);
//            $res[$vo['type_name']][] = $vo;
        }
        return $menu;
    }

    /*
     * 今日秒杀
     *
     */
    public function _seckill()
    {
    
        return [];
    }


    public function img($img)
    {
        $rTotal = 0;
        $gTotal = 0;
        $bTotal = 0;
        $total = 0;
        $i = imagecreatefrompng($img);
        if (!$i) {
            $i = imagecreatefromjpeg($img);
        }
        for ($x = 0; $x < imagesx($i); $x++) {
            for ($y = 0; $y < imagesy($i); $y++) {
                $rgb = imagecolorat($i, $x, $y);
                $r = ($rgb >> 16) & 0xFF;
                $g = ($rgb >> 8) & 0xFF;
                $b = $rgb & 0xFF;
                $rTotal += $r;
                $gTotal += $g;
                $bTotal += $b;
                $total++;
            }
        }
        $rAverage = round($rTotal / $total);
        $gAverage = round($gTotal / $total);
        $bAverage = round($bTotal / $total);
        return [$rAverage, $gAverage, $bAverage];
    }

    /*
     * 热卖榜单
     *
     */
    public function _hot()
    {
        $id_str = '0';
        $ids_arr = Db::name("goods_hot_top")->column("category_id");
        if ($ids_arr) {
            $id_str = implode(',', $ids_arr);
        }
        $where = " cid in ({$id_str}) ";

        $res = Goods::field("id,name,thumb")
            ->where($where)
            ->limit(2)
            ->order("sales_sum desc")
            ->select();
      
        foreach ($res as &$v) {
            $v['thumb'] = get_file_url($v['thumb']);
        }      
        return $res;
    }

    /*
     * 新品首发
     *
     */
    public function _new()
    {
        $res = Goods::where([
                'is_sale' => 1,
                'is_delete' => 0,
                'status' => 1,
                'is_new' => 1

            ])
            ->field("id,name,thumb")
            ->limit(2)
            ->order("update_time desc")
            ->select();   
        foreach ($res as &$v) {
            $v['thumb'] = get_file_url($v['thumb']);
        }        
        return $res;
    }

    /*
     * 新人0元购
     *
     */

    public function _buy($data, $user)
    {  
        
        return [];
    }

    /*
     * 首页底部
     *
     */
    public function bottom($data, $user)
    {
        $res = $this->_like();

        return ApiReturn::r(1, $res, lang('首页底部数据'));
    }

    /*
     * 猜你喜欢
     *
     */
    public function _like()
    {
        $res = Goods::where([
            'is_sale' => 1,
            'is_delete' => 0,
            'status' => 1,
            'is_recommend' => 1
        ])
        ->field("id,name,thumb,shop_price,market_price")
        ->paginate()->each(function (&$v) {
            $v['count'] = \app\order\model\OrderGoods::where('goods_id',$v["id"])->count("DISTINCT order_sn");
            $v['thumb'] = get_thumb($v['thumb']);
        });
        return $res;
    }

    /*
     * 每日分享
     *
     */
    public function _share()
    {
        $res = Goods::where([
            'is_sale' => 1,
            'is_delete' => 0,
            'status' => 1,
            'is_hot' => 1
        ])
            ->field("id,name,thumb,shop_price,market_price")
            ->paginate()->each(function (&$v) {
                $v['thumb'] = get_file_url($v['thumb']);
            });
        return $res;
    }
}
